home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / misc / toolboxsas.lha / Toolbox / lib / rex / Scanner.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  15.8 KB  |  417 lines

  1. /* $Id: Scanner.c,v 2.16 1992/08/18 09:05:32 grosch rel $ */
  2.  
  3. # define bool        char
  4. # define true        1
  5. # define false        0
  6. # define StdIn        0
  7.  
  8. $@ # include "@.h"
  9. $@ # include "$Source.h"
  10. # include "System.h"
  11. # include "General.h"
  12. # include "DynArray.h"
  13. # include "Positions.h"
  14. # include <stdio.h>
  15.  
  16. #  include <stdlib.h>
  17.  
  18. # define yyTabSpace    8
  19.  
  20. # define yyStart(State)    { yyPreviousStart = yyStartState; yyStartState = State; }
  21. # define yyPrevious    { yyStateRange s = yyStartState; \
  22.              yyStartState = yyPreviousStart; yyPreviousStart = s; }
  23. $@ # define yyEcho        { char * yyEnd = $_TokenPtr + $_TokenLength; \
  24.             char yyCh = * yyEnd; * yyEnd = '\0'; \
  25. $@              (void) fputs ($_TokenPtr, stdout); * yyEnd = yyCh; }
  26. $@ # define yyLess(n)    { yyChBufferIndex -= $_TokenLength - n; $_TokenLength = n; }
  27. $@ # define yyTab        yyLineStart -= yyTabSpace - 1 - ((unsigned char *) $_TokenPtr - yyLineStart - 1) % yyTabSpace
  28. $@ # define yyTab1(a)    yyLineStart -= yyTabSpace - 1 - ((unsigned char *) $_TokenPtr - yyLineStart + a - 1) % yyTabSpace
  29. $@ # define yyTab2(a,b)    yyLineStart -= yyTabSpace - 1 - ((unsigned char *) $_TokenPtr - yyLineStart + a - 1) % yyTabSpace
  30. # define yyEol(Column)    { yyLineCount ++; yyLineStart = yyChBufferIndex - 1 - Column; }
  31. # define output(c)    (void) putchar ((int) c)
  32. # define unput(c)    * (-- yyChBufferIndex) = c
  33.  
  34. # define yyDNoState        0
  35. # define yyFileStackSize    16
  36. # define yyInitBufferSize    1024 * 8 + 256
  37. $C constant declarations
  38.  
  39. static void yyExit (void) { Exit (1); }
  40.  
  41. typedef unsigned short    yyStateRange;
  42. typedef struct { yyStateRange yyCheck, yyNext; } yyCombType;
  43.  
  44. $@     char *        $_TokenPtr    ;
  45. $@     short        $_TokenLength    ;
  46. $@     $_tScanAttribute    $_Attribute    ;
  47. $@     void        (* @_Exit) (void) = yyExit;
  48.  
  49. static    yyCombType    yyComb        [yyTableSize   + 1] = {{1, 0},
  50. $T
  51. };
  52. static    yyCombType *    yyBasePtr    [yyDStateCount + 1] = {& yyComb [0],
  53. $B
  54. };
  55. static    yyStateRange    yyDefault    [yyDStateCount + 1] = {0,
  56. $U
  57. };
  58. static    yyStateRange    yyEobTrans    [yyDStateCount + 1] = {0,
  59. $R
  60. };
  61. $M static short        yyAction    [yyDStateCount + 1] = {0,
  62. $Q
  63. $M };
  64.  
  65. static    yyStateRange    yyInitStateStack [yyInitBufferSize] = {0};
  66. static    yyStateRange *    yyStateStack    = yyInitStateStack;
  67. static    unsigned long    yyStateStackSize= yyInitBufferSize;
  68. static    yyStateRange    yyStartState    = 0;
  69. static    yyStateRange    yyPreviousStart    = 1;
  70.  
  71. static  short        yySourceFile    ;
  72. static    bool        yyEof        ;
  73. static    unsigned char *    yyChBufferPtr    ;
  74. static    unsigned char *    yyChBufferStart    ;
  75. static    unsigned long    yyChBufferSize    ;
  76. static    unsigned char *    yyChBufferIndex    = ((unsigned char *) yyComb) + 2; /* dirty trick */
  77. static    int        yyBytesRead    ;
  78. static    int        yyLineCount    ;
  79. static    unsigned char *    yyLineStart    ;
  80.  
  81. static    struct {
  82.     short        yySourceFile    ;
  83.     bool        yyEof        ;
  84.     unsigned char *    yyChBufferPtr    ;
  85.     unsigned char *    yyChBufferStart    ;
  86.     unsigned long    yyChBufferSize    ;
  87.     unsigned char *    yyChBufferIndex    ;
  88.     int        yyBytesRead    ;
  89.     int        yyLineCount    ;
  90.     unsigned char *    yyLineStart    ;
  91.     } yyFileStack [yyFileStackSize + 1], * yyFileStackPtr = yyFileStack;
  92.  
  93. static    char    yyToLower    [] = {
  94. '\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7',
  95. '\10', '\11', '\12', '\13', '\14', '\15', '\16', '\17',
  96. '\20', '\21', '\22', '\23', '\24', '\25', '\26', '\27',
  97. '\30', '\31', '\32', '\33', '\34', '\35', '\36', '\37',
  98. ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
  99. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
  100. '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  101. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', '\\', ']', '^', '_',
  102. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  103. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\177',
  104. '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  105. '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
  106. '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
  107. '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
  108. '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
  109. '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
  110. '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  111. '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  112. '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
  113. '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
  114. '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
  115. '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
  116. '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  117. '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  118. '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  119. '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
  120. };
  121.  
  122. static    char    yyToUpper    [] = {
  123. '\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7',
  124. '\10', '\11', '\12', '\13', '\14', '\15', '\16', '\17',
  125. '\20', '\21', '\22', '\23', '\24', '\25', '\26', '\27',
  126. '\30', '\31', '\32', '\33', '\34', '\35', '\36', '\37',
  127. ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
  128. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
  129. '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  130. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
  131. '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  132. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', '\177',
  133. '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  134. '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
  135. '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
  136. '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
  137. '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
  138. '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
  139. '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  140. '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  141. '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
  142. '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
  143. '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
  144. '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
  145. '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  146. '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  147. '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  148. '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
  149. };
  150.  
  151. static    void    yyInitialize    (void);
  152. static    void    yyErrorMessage    (int yyErrorCode);
  153. static    char    input        (void);
  154. $G global user declarations
  155.  
  156. $@ int $_GetToken (void)
  157. {
  158.    register    yyStateRange    yyState;
  159.    register    yyStateRange *    yyStatePtr;
  160.    register    unsigned char * yyChBufferIndexReg;
  161.    register    yyCombType * *    yyBasePtrReg = yyBasePtr;
  162. $L local user declarations and actions
  163.  
  164. yyBegin:
  165.    yyState        = yyStartState;        /* initialize */
  166.    yyStatePtr        = & yyStateStack [1];
  167.    yyChBufferIndexReg     = yyChBufferIndex;
  168. $@    $_TokenPtr         = (char *) yyChBufferIndexReg;
  169. $J if (yyChBufferIndexReg [-1] == yyEolCh) yyState ++;
  170.  
  171.    /* ASSERT yyChBuffer [yyChBufferIndex] == first character */
  172.  
  173. yyContinue:        /* continue after sentinel or skipping blanks */
  174.    for (;;) {        /* execute as many state transitions as possible */
  175.                     /* determine next state and get next character */
  176.       register yyCombType * yyTablePtr = (yyBasePtrReg [yyState] + * yyChBufferIndexReg ++);
  177.       if (yyTablePtr->yyCheck == yyState) {
  178.      yyState = yyTablePtr->yyNext;
  179.      * yyStatePtr ++ = yyState;        /* push state */
  180.      goto yyContinue;
  181.       }
  182.       yyChBufferIndexReg --;            /* reconsider character */
  183.       if ((yyState = yyDefault [yyState]) == yyDNoState) break;
  184.    }
  185.  
  186.    for (;;) {                    /* search for last final state */
  187. $@       $_TokenLength = yyChBufferIndexReg - (unsigned char *) $_TokenPtr;
  188.       yyChBufferIndex = yyChBufferIndexReg;
  189. $A switch header and user actions    /* switch (* -- yyStatePtr) {    /* pop state */
  190. $N non final states
  191.      /* non final states */
  192.      yyChBufferIndexReg --;            /* return character */
  193.      break;
  194.  
  195. $P    case yyDefaultState:
  196. $@      $_Attribute.Position.Line   = yyLineCount;
  197. $@      $_Attribute.Position.Column = yyChBufferIndexReg - yyLineStart;
  198. $@       /* $_TokenLength   = 1; */
  199.      yyChBufferIndex = ++ yyChBufferIndexReg;
  200.      {
  201. $D default action
  202.      }
  203.      goto yyBegin;
  204.  
  205.       case yyDNoState:                /* automatic initialization */
  206.      yyInitialize ();
  207.      yySourceFile = StdIn;
  208.      goto yyBegin;
  209.  
  210. $O    case yyEobState:                /* end of buffer sentinel found */
  211.      yyChBufferIndex = -- yyChBufferIndexReg; /* undo last state transition */
  212. $@      if (-- $_TokenLength == 0) {        /* get previous state */
  213.         yyState = yyStartState;
  214. $J        if (yyChBufferIndexReg [-1] == yyEolCh) yyState ++;
  215.      } else {
  216.         yyState = * (yyStatePtr - 1);
  217.      }
  218.  
  219.      if (yyChBufferIndex != & yyChBufferStart [yyBytesRead]) {
  220.                         /* end of buffer sentinel in buffer */
  221.         if ((yyState = yyEobTrans [yyState]) == yyDNoState) continue;
  222.         yyChBufferIndexReg ++;
  223.         * yyStatePtr ++ = yyState;        /* push state */
  224.         goto yyContinue;
  225.      }
  226.                         /* end of buffer reached */
  227.      {  /* copy initial part of token in front of the input buffer */
  228. $@         register char * yySource = $_TokenPtr;
  229. $@         register char * yyTarget = $_TokenPtr = (char *) & yyChBufferPtr [yyMaxAlign - $_TokenLength % yyMaxAlign];
  230.         if (yySource != yyTarget) {
  231.            while (yySource < (char *) yyChBufferIndexReg) * yyTarget ++ = * yySource ++;
  232.            yyLineStart += (unsigned char *) yyTarget - yyChBufferStart - yyBytesRead;
  233.            yyChBufferStart = (unsigned char *) yyTarget;
  234.         } else {
  235.            yyChBufferStart = yyChBufferIndexReg;
  236.         }
  237.      }
  238.  
  239.      if (! yyEof) {                /* read buffer and restart */
  240. $@         int yyChBufferFree = (int) Exp2 (Log2 (yyChBufferSize - 4 - yyMaxAlign - $_TokenLength));
  241.         if (yyChBufferFree < yyChBufferSize / 8) {
  242.            register int yyDelta;
  243.            register unsigned char * yyOldChBufferPtr = yyChBufferPtr;
  244.            ExtendArray ((char * *) & yyChBufferPtr, & yyChBufferSize, sizeof (char));
  245.            if (yyChBufferPtr == NULL) yyErrorMessage (1);
  246.            yyDelta = yyChBufferPtr - yyOldChBufferPtr;
  247.            yyChBufferStart    += yyDelta;
  248.            yyLineStart    += yyDelta;
  249. $@            $_TokenPtr    += yyDelta;
  250. $@            yyChBufferFree = (int) Exp2 (Log2 (yyChBufferSize - 4 - yyMaxAlign - $_TokenLength));
  251.            if (yyStateStackSize < yyChBufferSize) {
  252.           yyStateRange * yyOldStateStack = yyStateStack;
  253.           ExtendArray ((char * *) & yyStateStack, & yyStateStackSize, sizeof (yyStateRange));
  254.           if (yyStateStack == NULL) yyErrorMessage (1);
  255.           yyStatePtr    += yyStateStack - yyOldStateStack;
  256.            }
  257.         }
  258.         yyChBufferIndex = yyChBufferIndexReg = yyChBufferStart;
  259. $@         yyBytesRead = $_GetLine (yySourceFile, (char *) yyChBufferIndex, yyChBufferFree);
  260.         if (yyBytesRead <= 0) { yyBytesRead = 0; yyEof = true; }
  261.         yyChBufferStart [yyBytesRead    ] = yyEobCh;
  262.         yyChBufferStart [yyBytesRead + 1] = '\0';
  263.         goto yyContinue;
  264.      }
  265.  
  266. $@      if ($_TokenLength == 0) {        /* end of file reached */
  267. $@         $_Attribute.Position.Line   = yyLineCount;
  268. $@         $_Attribute.Position.Column = yyChBufferIndexReg - yyLineStart;
  269. $@         $_CloseFile ();
  270.         if (yyFileStackPtr == yyFileStack) {
  271. $E eof action
  272.         }
  273. $@         if (yyFileStackPtr == yyFileStack) return $_EofToken;
  274.         goto yyBegin;
  275.      }
  276.      break;
  277.  
  278.       default:
  279.      yyErrorMessage (0);
  280.       }
  281.    }
  282. }
  283.  
  284. $@ void $_BeginFile(char * yyFileName)
  285.    {
  286.       yyInitialize ();
  287. $@       yySourceFile = $_BeginSource (yyFileName);
  288.    }
  289.  
  290. static void yyInitialize (void)
  291.    {
  292.       if (yyFileStackPtr >= yyFileStack + yyFileStackSize) yyErrorMessage (2);
  293.       yyFileStackPtr ++;            /* push file */
  294.       yyFileStackPtr->yySourceFile    = yySourceFile        ;
  295.       yyFileStackPtr->yyEof        = yyEof            ;
  296.       yyFileStackPtr->yyChBufferPtr    = yyChBufferPtr        ;
  297.       yyFileStackPtr->yyChBufferStart    = yyChBufferStart    ;
  298.       yyFileStackPtr->yyChBufferSize    = yyChBufferSize    ;
  299.       yyFileStackPtr->yyChBufferIndex    = yyChBufferIndex    ;
  300.       yyFileStackPtr->yyBytesRead    = yyBytesRead        ;
  301.       yyFileStackPtr->yyLineCount    = yyLineCount        ;
  302.       yyFileStackPtr->yyLineStart    = yyLineStart        ;
  303.                         /* initialize file state */
  304.       yyChBufferSize       = yyInitBufferSize;
  305.       MakeArray ((char * *) & yyChBufferPtr, & yyChBufferSize, sizeof (char));
  306.       if (yyChBufferPtr == NULL) yyErrorMessage (1);
  307.       yyChBufferStart       = & yyChBufferPtr [yyMaxAlign];
  308.       yyChBufferStart [-1] = yyEolCh;        /* begin of line indicator */
  309.       yyChBufferStart [ 0] = yyEobCh;        /* end of buffer sentinel */
  310.       yyChBufferStart [ 1] = '\0';
  311.       yyChBufferIndex       = yyChBufferStart;
  312.       yyEof           = false;
  313.       yyBytesRead       = 0;
  314.       yyLineCount       = 1;
  315.       yyLineStart       = & yyChBufferStart [-1];
  316.       if (yyStartState == 0) {
  317.      yyStartState       = STD;
  318.      yyStateStack [0]  = yyDefaultState;    /* stack underflow sentinel */
  319.       }
  320.    }
  321.  
  322. $@ void $_CloseFile (void)
  323.    {
  324.       if (yyFileStackPtr == yyFileStack) yyErrorMessage (3);
  325. $@       $_CloseSource (yySourceFile);
  326.       ReleaseArray ((char * *) & yyChBufferPtr, & yyChBufferSize, sizeof (char));
  327.                         /* pop file */
  328.       yySourceFile    = yyFileStackPtr->yySourceFile        ;
  329.       yyEof        = yyFileStackPtr->yyEof            ;
  330.       yyChBufferPtr    = yyFileStackPtr->yyChBufferPtr        ;
  331.       yyChBufferStart    = yyFileStackPtr->yyChBufferStart    ;
  332.       yyChBufferSize    = yyFileStackPtr->yyChBufferSize    ;
  333.       yyChBufferIndex    = yyFileStackPtr->yyChBufferIndex    ;
  334.       yyBytesRead    = yyFileStackPtr->yyBytesRead        ;
  335.       yyLineCount    = yyFileStackPtr->yyLineCount        ;
  336.       yyLineStart    = yyFileStackPtr->yyLineStart        ;
  337.       yyFileStackPtr --;
  338.    }
  339.  
  340. $@ int $_GetWord(char * yyWord)
  341.    {
  342. $@       register char * yySource            = $_TokenPtr;
  343.       register char * yyTarget            = yyWord;
  344.       register char * yyChBufferIndexReg    = (char *) yyChBufferIndex;
  345.  
  346.       do {                /* ASSERT word is not empty */
  347.      * yyTarget ++ = * yySource ++;
  348.       } while (yySource < yyChBufferIndexReg);
  349.       * yyTarget = '\0';
  350. $@       return yyChBufferIndexReg - $_TokenPtr;
  351.    }
  352.  
  353. $@ int $_GetLower(char * yyWord)
  354.    {
  355. $@       register char * yySource            = $_TokenPtr;
  356.       register char * yyTarget            = yyWord;
  357.       register char * yyChBufferIndexReg    = (char *) yyChBufferIndex;
  358.  
  359.       do {                /* ASSERT word is not empty */
  360.      * yyTarget ++ = yyToLower [* yySource ++];
  361.       } while (yySource < yyChBufferIndexReg);
  362.       * yyTarget = '\0';
  363. $@       return yyChBufferIndexReg - $_TokenPtr;
  364.    }
  365.  
  366. $@ int $_GetUpper(char * yyWord)
  367.    {
  368. $@       register char * yySource            = $_TokenPtr;
  369.       register char * yyTarget            = yyWord;
  370.       register char * yyChBufferIndexReg    = (char *) yyChBufferIndex;
  371.  
  372.       do {                /* ASSERT word is not empty */
  373.      * yyTarget ++ = yyToUpper [* yySource ++];
  374.       } while (yySource < yyChBufferIndexReg);
  375.       * yyTarget = '\0';
  376. $@       return yyChBufferIndexReg - $_TokenPtr;
  377.    }
  378.  
  379. static char input (void)
  380.    {
  381.       if (yyChBufferIndex == & yyChBufferStart [yyBytesRead]) {
  382.      if (! yyEof) {
  383.         yyLineStart -= yyBytesRead;
  384.         yyChBufferIndex = yyChBufferStart = yyChBufferPtr;
  385. $@         yyBytesRead = $_GetLine (yySourceFile, (char *) yyChBufferIndex,
  386.            (int) Exp2 (Log2 (yyChBufferSize)));
  387.         if (yyBytesRead <= 0) { yyBytesRead = 0; yyEof = true; }
  388.         yyChBufferStart [yyBytesRead    ] = yyEobCh;
  389.         yyChBufferStart [yyBytesRead + 1] = '\0';
  390.      }
  391.       }
  392.       if (yyChBufferIndex == & yyChBufferStart [yyBytesRead]) return '\0';
  393.       else return((char)(*yyChBufferIndex++));
  394.    }
  395.  
  396. $@ void $_BeginScanner (void)
  397.    {
  398. $I user initialization code
  399.    }
  400.  
  401. $@ void $_CloseScanner (void)
  402.    {
  403. $F user finalization code
  404.    }
  405.  
  406. static void yyErrorMessage(int yyErrorCode)
  407.    {
  408. $@       WritePosition (stderr, $_Attribute.Position);
  409.       switch (yyErrorCode) {
  410. $@       case 0: (void) fprintf (stderr, ": @: internal error\n"); break;
  411. $@       case 1: (void) fprintf (stderr, ": @: out of memory\n"); break;
  412. $@       case 2: (void) fprintf (stderr, ": @: too many nested include files\n"); break;
  413. $@       case 3: (void) fprintf (stderr, ": @: file stack underflow (too many calls of $_CloseFile)\n"); break;
  414.       }
  415. $@      (*@_Exit)();
  416.    }
  417.